import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import {FORM_OPTIONS} from '../constants';
import TextInput from 'Shared/components/TextInput';
import {hasPermissions} from './utils';
import {PERMISSION} from './constants';
import ImportPanel from './ImportPanel';
const ButtonGroup = Button.Group;
const FormItem = Form.Item;

class QueryPanel extends React.PureComponent {
    state = {
        showImportPanel: false
    }
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }

    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    onSelectDealer = data => {
        this.props.onConditionsChange('dealerId', data.id);
        this.props.onQueryDealerChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
    }
    onChangeImport = () => {
        this.setState({
            showImportPanel: true
        });
    }
    onImportChange = data => {
        if(data)
            this.setState({
                showImportPanel: false
            });
    }
    onCloseImportPanel = () => {
        this.setState({
            showImportPanel: false
        });
    }
    render() {
        const {initData} = this.props;
        const {formatMessage} = this.props.intl;
        const otherBtns = [];
        if(hasPermissions(this.props.permissions, PERMISSION.import))
            otherBtns.push(
                <Button key={PERMISSION.import} onClick={this.onChangeImport}>
                    {this.props.intl.formatMessage({
                        id: 'queryPanel.btn.mode',
                        defaultMessage: '导入'
                    })}
                </Button>
            );
        return (
            <div>
                <Form className="form-standard">
                    <Row>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.dealerCode',
                                    defaultMessage: '经销商编号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="dealerCode"
                                    disabled={initData && initData.isSubDealerUser}
                                    value={this.props.conditions.dealerCode}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.dealerName',
                                    defaultMessage: '经销商名称'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="dealerName"
                                    disabled={initData && initData.isSubDealerUser}
                                    value={this.props.conditions.dealerName}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                        {!initData.isSubDealerUser && <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryPanel.form.shortCode',
                                defaultMessage: '简码'
                            })}{...FORM_OPTIONS.item}>
                                <TextInput
                                    name="shortCode"
                                    value={this.props.conditions.shortCode}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>}
                        {!initData.isSubDealerUser && <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryPanel.form.shortName',
                                defaultMessage: '简称'
                            })}{...FORM_OPTIONS.item}>
                                <TextInput
                                    name="shortName"
                                    value={this.props.conditions.shortName}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>}
                    </Row>
                    <Row>
                        <Col span={6}>
                            <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>{formatMessage({
                                id: 'queryPanel.btn.query',
                                defaultMessage: '查询'
                            })}</Button>
                            <Button key="clear" onClick={this.props.onResetClick}>{formatMessage({
                                id: 'queryPanel.btn.reset',
                                defaultMessage: '重置'
                            })}</Button>
                        </Col>
                        <Col span={18} className="col-align-right">
                            <ButtonGroup>{otherBtns}</ButtonGroup>
                        </Col>
                    </Row>
                </Form>
                {
                    this.state.showImportPanel &&
                    <ImportPanel
                        selectKey={'1'}
                        onCancel={this.onCloseImportPanel}
                        onImportChange={this.onImportChange} />
                }
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onQueryDealerChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    initData: PropTypes.object,
    intl: PropTypes.object,
    onChangeImport: PropTypes.func,

};

import {connect} from 'react-redux';
import {saveQueryCondition, querySearch, resetQueryPanel, saveQueryDealer} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {injectIntl} from './intl';

const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getInitData = selectorFactory(['page', 'domainData', 'initData']);
const getQueryDealer = selectorFactory(['page', 'appState', 'queryDealer']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    queryDealer: getQueryDealer(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    permissions: getPermissions(state),
    initData: getInitData(state)
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    onQueryDealerChange: data => dispatch(saveQueryDealer(data)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
